$(function () {
    var userInfo = LoginStatus()
    //渲染熟练度
    var selectArr =  SelectAjaxRetrun('/user/prop/skillProficiency/list?page=0&limit=10&name=','get','name');
    if(selectArr == ""){
        return false;
    }
    $("#proficiency").append(selectArr)
    // 点击
    $(".skill-part .add-btn").click(function () {
        $(".itemselect").removeAttr("lay-filter");
        regiAddSkill($(this),selectArr);
        useLayUI();
    })
    $(".exper-part .add-btn").click(function () {
        regiAddProExper($(this));
        useLayUI();
    })

    useLayUI();
    //提交
    $("#Submitbtn").click(function(){
        var professionalSkills = [];
        //技能
        var skillNames = [];
        var skillName = $("input[name='skillName[]']").each(function(){
            skillNames.push($(this).val())
            // console.log()
        });
        //熟练度
        var proficiencys = [];
        var proficiency = $("select[name='proficiency[]']").each(function(){
            proficiencys.push($(this).val())
        });
        for(var i=0;i<skillNames.length;i++){
            if(skillNames[i]==""){
                console.log(proficiencys[i])
                if(proficiencys[i]!=""){
                    layerMsg("若存在技能则技能名称不为空")
                    return false;
                }
            }else{
                var jsondata = {'skillName':skillNames[i],
                'proficiency':proficiencys[i]
                }
                professionalSkills.push(jsondata)
            }
        }
        
        //工作经验
        var experienceProjects = [];
        //项目名称
        var projectNames = [];
        var projectName = $("input[name='projectName[]']").each(function(){
            projectNames.push($(this).val())
            // console.log()
        });
        //项目职责
        var duties = [];
        var dutie = $("input[name='duties[]']").each(function(){
            duties.push($(this).val())
        });
        //描述
        var workContexts = [];
        var workContext = $("textarea[name='workContext[]']").each(function(){
            workContexts.push($(this).val())
        });
        for(var i=0;i<projectNames.length;i++){
            if(projectNames[i]==""){
                if(duties[i]!=""||workContexts[i]!=""){
                    layerMsg("若存在项目则项目名称不为空")
                    return false;
                }
            }else{
                var jsondata = {'projectName':projectNames[i],
                'duties':duties[i],
                'workContext':workContexts[i],
                }
                experienceProjects.push(jsondata)
            }

        }
        console.log(experienceProjects)
        // if(experienceProjects.length=0){
        //     experienceProjects = null;
        // }
        //一句话描述自己
        var profile = $("textarea[name='profile']").val();
        var resume = JSON.stringify({
            "name":"个人简历"	//简历名称
			,"description":null
			,"profile":profile

        });
        
        //工作经验
        var experienceWork = $.cookie('experienceWork')
        if(experienceWork==null||experienceWork==""){
            experienceWork = [];
        }
    
        var experienceEducations = $.cookie('experienceEducations')
        if(experienceEducations==null||experienceEducations==""){
            experienceEducations = [];
        }
        //求职意向
        var careerObjectives = $.cookie('careerObjectives')
        if(careerObjectives==null||careerObjectives==""){
            careerObjectives = [];
        }
        if(experienceWork.length==0){
            var data = {
                'resume':JSON.parse(resume),
                'experienceWork':experienceWork,
                'experienceEducations':JSON.parse(experienceEducations),
                'careerObjectives':JSON.parse(careerObjectives),
                'professionalSkills':professionalSkills,
                'experienceProjects':experienceProjects,
            }
        }else{
            var data = {
                'resume':JSON.parse(resume),
                'experienceWork':JSON.parse(experienceWork),
                'experienceEducations':JSON.parse(experienceEducations),
                'careerObjectives':JSON.parse(careerObjectives),
                'professionalSkills':professionalSkills,
                'experienceProjects':experienceProjects,
            }
        }
        $.ajax({
            type: 'put', // 提交方式 get/post
            url: services+"/user/resume", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
				withCredentials: true
			}
            ,crossDomain: true
            ,data:JSON.stringify(data),
             success: function(res) {
                result = JSON.stringify(res);
                ApiLoginStatus(result)
                if(res.success){
                    layerMsg(res.message);
                    setTimeout(function() {
                        $.cookie('experienceWork',{ expires: -1 })
                        $.cookie('careerObjectives',{ expires: -1 })
                        $.cookie('experienceEducations',{ expires: -1 })
                        $.cookie('oneStep',{ expires: -1 })
                        window.location.href="my-resume.html" ;
                    },3000);
                }       
            },
        })
    })

})

//方法

//下拉列表ajax
function SelectAjaxRetrun(_url,_type,_name){
    _url = services+_url;
    var selectArr = "";
    $.ajax({
        type: _type, // 提交方式 get/post
        url:_url,
        async:false,
        // contentType:"application/json;charset=utf-8",
        //解决跨域
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function(res) {
            var obj = JSON.parse(res);
            if(obj.code==0){
                data = obj.data;
                for(var i=0;i<data.length;i++){
                    selectArr = selectArr + "<option value='"+data[i].id+"'>"+data[i][_name]+"</option>";
                }
                // console.log(1+selectArr)
            }else{
                layerMsg("加载失败，请重试")
            }
        }
    })
    
    // console.log(selectArr)
    return selectArr;
}

//动态添加专业技能（注册）
function regiAddSkill(obj,html) { 
    var skill = '<ul>\n' +
        '                <li>\n' +
        '                    <span>技能名称</span><input type="text" name="skillName[]" placeholder="请输入技能名称"/>\n' +
        '                </li>\n' +
        '                <li class="layui-form itemselect" lay-filter="select" >\n' +
        '                    <span>熟悉程度</span>\n' +
        '                    <div class="layui-form-item" >\n' +
        '                        <select name="proficiency[]"   >\n' +
        '<option value="">请选择</option>\n'
        +html
        
        '                        </select>\n' +
        '                    </div>\n' +
        '                </li>\n' +
        '            </ul>';

    
    $(skill).insertBefore(obj);
}
